package com.baizhi.rjb.service;

import com.baizhi.rjb.dao.CategoryMapper;
import com.baizhi.rjb.dto.CategoryAddDto;
import com.baizhi.rjb.dto.CategoryDelDto;
import com.baizhi.rjb.dto.CategoryUpdDto;
import com.baizhi.rjb.entity.Category;
import com.baizhi.rjb.entity.CategoryExample;
import com.baizhi.rjb.vo.CategoryGoryVo;
import com.baizhi.rjb.vo.CategoryStaVo;
import com.baizhi.rjb.vo.CategoryVo;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service("categoryService")
@Transactional
public class CategoryServiceImpl implements CategoryService{
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public CategoryVo page(int page, int pageSize) {
        RowBounds rowBounds = new RowBounds((page-1)*pageSize,pageSize);
        CategoryExample categoryExample=new CategoryExample();
        categoryExample.createCriteria().andLevelsEqualTo(1);
        List<Category> categories = categoryMapper.selectByExampleAndRowBounds(categoryExample, rowBounds);
        int count = categoryMapper.selectCountByExample(categoryExample);
        CategoryVo categoryVo = new CategoryVo(page, count, categories);
        return categoryVo;
    }

    @Override
    public CategoryStaVo add(CategoryAddDto categoryAddDto) {
        CategoryStaVo categoryAddVo = new CategoryStaVo();
        try {
            if(categoryAddDto.getParentId()==null){
                categoryMapper.insert(new Category(null,categoryAddDto.getCateName(),1,null));
            }else {
                categoryMapper.insert(new Category(null,categoryAddDto.getCateName(),2,categoryAddDto.getParentId()));
            }
            categoryAddVo.setMessage("添加成功");
            categoryAddVo.setStatus(200);
        }catch (Exception e){
            e.printStackTrace();
            categoryAddVo.setMessage("添加失败");
            categoryAddVo.setStatus(400);
        }
        return categoryAddVo;
    }

    @Override
    public CategoryStaVo delete(CategoryDelDto categoryDelDto) {
        CategoryStaVo categoryStaVo =new CategoryStaVo();

        if(categoryDelDto.getParentId()==null){
            CategoryExample categoryExample=new CategoryExample();
            categoryExample.createCriteria().andParentIdEqualTo(categoryDelDto.getId());
            int i = categoryMapper.selectCountByExample(categoryExample);
            if(i==0){
                categoryMapper.deleteByPrimaryKey(categoryDelDto.getId());
                categoryStaVo.setMessage("删除成功");
                categoryStaVo.setStatus(200);
            }else {
                categoryStaVo.setMessage("一级类别下有二级类别不能删除");
                categoryStaVo.setStatus(400);
            }
        }else {
            categoryMapper.deleteByPrimaryKey(categoryDelDto.getId());
            categoryStaVo.setMessage("删除成功");
            categoryStaVo.setStatus(200);
        }
        return categoryStaVo;
    }

    @Override
    public Category queryOne(int id) {
        Category category = new Category();
        category.setId(id);
        return categoryMapper.selectOne(category);
    }

    @Override
    public CategoryStaVo update(CategoryUpdDto categoryUpdDto) {
        CategoryStaVo categoryStaVo=new CategoryStaVo();
        try {
            Category category = new Category();
            category.setId(categoryUpdDto.getId());
            category.setCateName(categoryUpdDto.getCateName());
            category.setParentId(categoryUpdDto.getParentId());
            CategoryExample categoryExample = new CategoryExample();
            categoryExample.createCriteria().andIdEqualTo(categoryUpdDto.getId());
            categoryMapper.updateByExampleSelective(category, categoryExample);
            categoryStaVo.setMessage("修改成功");
            categoryStaVo.setStatus(200);
        }catch (Exception e){
            e.printStackTrace();
            categoryStaVo.setMessage("修改失败");
            categoryStaVo.setStatus(400);
        }
        return categoryStaVo;
    }

    @Override
    public CategoryVo page(int page, int pageSize, int parentId) {
        RowBounds rowBounds = new RowBounds((page-1)*pageSize,pageSize);
        CategoryExample categoryExample=new CategoryExample();
        categoryExample.createCriteria().andParentIdEqualTo(parentId);
        List<Category> categories = categoryMapper.selectByExampleAndRowBounds(categoryExample, rowBounds);
        int count = categoryMapper.selectCountByExample(categoryExample);
        CategoryVo categoryVo = new CategoryVo(page, count, categories);
        return categoryVo;
    }

    @Override
    public List<CategoryGoryVo> cate(int levels) {
        List<CategoryGoryVo>  GoryVo=new ArrayList<>();
        CategoryExample categoryExample=new CategoryExample();
        categoryExample.createCriteria().andLevelsEqualTo(levels);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        for (Category category : categories) {
            GoryVo.add(new CategoryGoryVo(category.getId(), category.getCateName()));
        }
        return GoryVo;
    }
}

